package cn.stylefeng.guns.modular.ums.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.constants.PayStatus;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.entity.MemberVipOrders;
import cn.stylefeng.guns.modular.ums.entity.Vip;
import cn.stylefeng.guns.modular.ums.mapper.MemberVipOrdersMapper;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.model.params.MemberVipOrdersParam;
import cn.stylefeng.guns.modular.ums.model.result.MemberVipOrdersResult;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import  cn.stylefeng.guns.modular.ums.service.MemberVipOrdersService;
import cn.stylefeng.guns.modular.ums.service.VipService;
import cn.stylefeng.guns.utils.DateUtils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ijpay.core.kit.WxPayKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * VIP充值记录表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2019-11-07
 */
@Service
public class MemberVipOrdersServiceImpl extends ServiceImpl<MemberVipOrdersMapper, MemberVipOrders> implements MemberVipOrdersService {
    @Autowired
    private VipService vipService;
    @Autowired
    private MemberService memberService;

    @Override
    public void add(MemberVipOrdersParam param){
        MemberVipOrders entity = getEntity(param);
        this.save(entity);
    }

    @Override
    @Transactional
    public void addOrderByVip(Vip vip,Integer memberId,String orderSn) {
        MemberVipOrdersParam orders=new MemberVipOrdersParam();
        orders.setOrdersSn(orderSn);
        orders.setMemberId(memberId);
        orders.setNote("购买VIP-"+vip.getName());
        orders.setPayAmount(vip.getPrice());
        orders.setPayType(2);//支付方式	0未支付	1支付宝	2微信	3余额	4银联
        orders.setOrdersStatus(0);//0-待付款	1-已付款	2-已关闭	3-无效订单
        orders.setVipId(vip.getVipType());//vip标号，非自增ID
        orders.setVipName(vip.getName());
        add(orders);
    }

    @Override
    public void delete(MemberVipOrdersParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional
    public void update(MemberVipOrdersParam param){
        MemberVipOrders oldEntity = getOldEntity(param);
        MemberVipOrders newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public void updateForPaySuccess(MemberVipOrders orders) {
        orders.setOrdersStatus(PayStatus.Paid.getValue());
        this.updateById(orders);
        //代码抽离成公共模块
        memberService.updateMemberVip(1,orders.getVipId(),orders.getMemberId());
//        //则需要修改会员VIP状态
//        MemberParam memberParam =new MemberParam();
//        memberParam.setMemberId(orders.getMemberId());
//        //需要判断先前是否为VIP
//        Member member=memberService.getById(orders.getMemberId());
//        Date endDate=null;//结束日期
//        if (member.getVipGrade()>0){//非普通会员
//            // 取等级高的
//            if (member.getVipGrade().intValue() < orders.getVipId().intValue()){
//                memberParam.setVipGrade(orders.getVipId());
//            }else{
//                memberParam.setVipGrade(member.getVipGrade());
//            }
//            switch (orders.getVipId()){
//                case 1://月度会员
//                    endDate=DateUtils.addDateDays(member.getVipEndDate(),30);
//                    break;
//                case 2://季度会员
//                    endDate=DateUtils.addDateMonths(member.getVipEndDate(),3);
//                    break;
//                case 3://年度会员
//                    endDate=DateUtils.addDateMonths(member.getVipEndDate(),12);
//                    break;
//                case 4://终身会员
//                    endDate=DateUtils.addDateYears(member.getVipEndDate(),100);
//                    break;
//            }
//            memberParam.setVipEndDate(endDate);
//        }else{
//            memberParam.setVipGrade(orders.getVipId());
//            switch (orders.getVipId()){
//                case 1://月度会员
//                    endDate=DateUtils.addDateDays(new Date(),30);
//                    break;
//                case 2://季度会员
//                    endDate=DateUtils.addDateMonths(new Date(),3);
//                    break;
//                case 3://年度会员
//                    endDate=DateUtils.addDateMonths(new Date(),12);
//                    break;
//                case 4://终身会员
//                    endDate=DateUtils.addDateYears(new Date(),100);
//                    break;
//            }
//            memberParam.setVipEndDate(endDate);
//        }
//        memberService.update(memberParam);
    }

    @Override
    public MemberVipOrdersResult findBySpec(MemberVipOrdersParam param){
        return null;
    }

    @Override
    public List<MemberVipOrdersResult> findListBySpec(MemberVipOrdersParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MemberVipOrdersParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MemberVipOrdersParam param){
        return param.getOrdersId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MemberVipOrders getOldEntity(MemberVipOrdersParam param) {
        return this.getById(getKey(param));
    }

    private MemberVipOrders getEntity(MemberVipOrdersParam param) {
        MemberVipOrders entity = new MemberVipOrders();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
